package com.cloudm.technician.ui.modules.cloudm_box.transfer

import android.os.Bundle
import android.view.MenuItem
import android.view.View
import com.cloudm.technician.R
import com.cloudm.technician.data.model.DeviceInfo
import com.cloudm.technician.extensions.addFragmentToActivity
import com.cloudm.technician.extensions.hideSoftKeyboard
import com.cloudm.technician.extensions.string
import com.cloudm.technician.ui.base.BaseActivity
import com.cloudm.technician.ui.widget.OnceClickListener
import kotlinx.android.synthetic.main.activity_transfer.*

/**
 * 作者: wesley
 * 时间: 2017/12/23
 * 描述: 移交界面
 */
class TransferActivity : BaseActivity() {
    private var info: DeviceInfo? = null
    private var channelId: String? = null
    private var orderNumber: String? = null
    private var url: String? = null
    private var fragment: TransferFragment? = null

    override val layoutResID: Int
        get() = R.layout.activity_transfer

    override val optionsMenuEnabled: Boolean
        get() = true

    override val menuRes: Int
        get() = R.menu.menu_search

    override fun handleBundle(bundle: Bundle) {
        info = bundle.getParcelable("info")
        channelId = bundle.getString("channelId", null)
        orderNumber = bundle.getString("orderNumber", null)
        url = bundle.getString("url", null)
    }

    override fun initVariables() {

    }

    override fun initViews(savedInstanceState: Bundle?) {
        setSupportActionBar(toolbar)
        displayHomeAsUpEnabled = false

        fragment = manager.findFragmentById(
                R.id.common_content) as? TransferFragment ?: TransferFragment.newInstance(info,
                channelId, orderNumber, url)
        addFragmentToActivity(manager, fragment)
    }

    override fun initListener() {
        llBack.setOnClickListener(object : OnceClickListener() {
            override fun onOnceClick(view: View) {
                finish()
            }
        })
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        return when (item?.itemId) {
            R.id.menu_search -> {
                hideSoftKeyboard()
                fragment?.search(searchValue.string())
                true
            }
            else -> false
        }
    }
}